package org.nod.atgm.model;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.nod.atgm.gameobjects.IGameObject;

import com.threed.jpct.CollisionEvent;
import com.threed.jpct.CollisionListener;
import com.threed.jpct.Object3D;
import com.threed.jpct.SimpleVector;

public class CollisionManager {
	
	protected static HashMap <Integer, IGameObject> worldObjects = new HashMap<Integer, IGameObject>();
	protected static HashMap <Integer, IGameObject> projectileObjects = new HashMap<Integer, IGameObject>();
	protected static HashMap <Integer, IGameObject> destroyableObjects = new HashMap<Integer, IGameObject>();
	public static SimpleVector movementVector = new SimpleVector();
	
	protected static List<IGameCollisionListener> gameCollisionListeners = new ArrayList<IGameCollisionListener>();
			
	public static void registerWorldObject(IGameObject worldObject) {
		worldObjects.put(worldObject.getObject().getID(),worldObject);
	}
	
	public static void registerDestroyableObject(IGameObject worldObject) {
		destroyableObjects.put(worldObject.getObject().getID(),worldObject);
	}

	public static void registerProjectile(IGameObject projectileObject) {
		projectileObjects.put(projectileObject.getObject().getID(),projectileObject);
	}
	
	public static void unregisterProjectile(IGameObject projectileObject) {
		projectileObjects.remove(projectileObject.getObject().getID());
	}
	
	public static void addGameCollisionListener(IGameCollisionListener collisionListener) {
		gameCollisionListeners.add(collisionListener);
	}
	
	public static void removeGameCollisionListener(IGameCollisionListener collisionListener) {
		gameCollisionListeners.remove(collisionListener);
	}
	
	public static boolean checkProjectileCollision(IGameObject projectileObject, SimpleVector direction, float step) {
//		movementVector.set(direction);
//		movementVector.x *= step;
//		movementVector.y *= step;
//		movementVector.z *= step;
//		if (!movementVector.equals(projectileObject.getObject().checkForCollisionSpherical(movementVector,0.7f))) {
//			handleProjectileWorldHit(projectileObject, null);
//			return true;
//		}
		int result = projectileObject.getObject().checkForCollision(direction,step);
		if (result != Object3D.NO_OBJECT) {
			IGameObject worldObject = worldObjects.get(result);
			if (worldObject != null) {
				handleProjectileWorldHit(projectileObject, worldObject);
				return true;
			}
			IGameObject destroyableObject = destroyableObjects.get(result);
			if (destroyableObject != null) {

				handleProjectileDestroyableHit(projectileObject, destroyableObject);
				return true;
			}
		}
		return false;
	}

	protected static void handleProjectileDestroyableHit(IGameObject projectileObject, IGameObject destroyableObject) {
		for (IGameCollisionListener collisionListener : gameCollisionListeners) {
			collisionListener.handleProjectileDestroyableCollisionEvent(new GameCollisionEvent(projectileObject,destroyableObject));
		}
	}

	protected static void handleProjectileWorldHit(IGameObject projectileObject, IGameObject worldObject) {
		for (IGameCollisionListener collisionListener : gameCollisionListeners) {
			collisionListener.handleProjectileWorldCollisionEvent(new GameCollisionEvent(projectileObject,worldObject));
		}
	}
	
}
